<?php
// 下面这句是为了部分独立程序服务，统一入口程序则在entry.php中已加载
include_once dirname(__FILE__).'/../_config.php';

/**
 * 在这里定义本目录下的特定变量
 */

// 模板主题
$__f_theme = cache_read('theme.php');
if(!empty($__f_theme)){
	$__theme_pth = THR_PTH.$__f_theme['mark'].'/';
	$__theme_dir = THR_DIR.$__f_theme['mark'].'/';
}else{
	$__theme_pth = THR_PTH.'_default/';
	$__theme_dir = THR_DIR.'_default/';
}

// 调试，临时用旧模板
if(!empty($_GET['theme'])){
	$__theme_pth = THR_PTH.$_GET['theme'].'/';
	$__theme_dir = THR_DIR.$_GET['theme'].'/';
}

$__tpl_pth = $__theme_pth;	// 前台主题，模板和样式在同一目录下
$__tpl_dir = $__theme_dir;	// 前台主题，模板和样式在同一目录下

// 语言文件
$_f_langs = cache_read('langs.php');
$__f_lang_setting = $_f_langs['lang_mark_list']['zh-cn'];
$__lang_pth = LNG_PTH.$__f_lang_setting['mark'].'/';
$__lang_dir = LNG_DIR.$__f_lang_setting['mark']."/";

// 加载基本类
include_once FUN_PTH.'plugin.inc.php';

// 插件缓存
$__f_plugins = cache_read('plugins.php');
VS::set('f_plugins', $__f_plugins);

// 加载语言包
$__l = ea_read_language(array('mark' => 'zh-cn'), array('st_common', ));

## 加载用户类
$__oUser = new User();
$__userinfo = $__oUser->get_session_cookie();
if(empty($__userinfo) || !is_array($__userinfo)){
	$__userinfo = array(
		'userid'	=> 0,
		'username'	=> '',
		'realname'	=> '',
		'lsttime'	=> 0,
	);
}
if(empty($__userinfo['nickname'])){$__userinfo['nickname'] = $__userinfo['username'];}
VS::set('userinfo', $__userinfo);
VS::update($__userinfo);

/*******************************************************************************
 * 菜单与所在位置
 *******************************************************************************/
$__menus = array();
// ×××××××× 以下两个变量貌似没有用了 ××××××××
/*$__module = array_pop(explode('/home/', $_SERVER['SCRIPT_NAME']));
$__module = array_shift(explode('/', $__module));
if(empty($__basename)){
	$__basename = str_replace(array('.php', '.mk', '.html'), '', basename($_SERVER['SCRIPT_NAME']));
}*/
$__pageinfo = array();
$__menu_highlight = '';
$__url_here = array();

/*******************************************************************************
 * 商品分类
 *******************************************************************************/

$__category_id = 0;
$__f_goods_cats = cache_read('goods_categorys.php');

// 页码
$__pagesize = 10;	// 每页记录数默认
$__page = isset($_GET['page']) ? max(intval($_GET['page']), 1) : 1;

/*******************************************************************************
 * 调试信息
 *******************************************************************************/

//define('DEBUG_PR', true);
//define('DEBUG_FB', true);

if(isset($_GET['superdebug']) && $_GET['superdebug'] == 'yes'){
	define('DEBUG_PR', true);
	if(isset($_GET['fb']) && $_GET['fb'] == 'yes'){
		define('DEBUG_FB', true);
	}
}

// if((defined('DEBUG_PR') && DEBUG_PR == true) || (defined('DEBUG_FB') && DEBUG_FB == true)){
// 	// 调试类
// 	##@include_once CLS_PTH.'tools/Trace.class.php';
// }

if(defined('DEBUG_PR') && DEBUG_PR == true){
	$GLOBALS['debug_info'][] = anchor('request');
	$GLOBALS['debug_info']['REQUEST'] = array('GET' => $_GET, 'POST' => $_POST);

	if(isset($_GET['userinfo']) && $_GET['userinfo'] == 'yes'){
		$GLOBALS['debug_info']['userinfo'] = array(anchor('userinfo'), 'userinfo' => $__userinfo);
	}
}

if(defined('DEBUG_FB') && DEBUG_FB == true){
	// 加载 FirePHP 调试服务端代码
	include_once LIB_PTH.'FirePHPCore/fb.php';

	FB::trace('_config.php');
	fb(array('GET' => $_GET, 'POST' => $_POST), 'REQUEST');

	if(isset($_GET['funcs']) && $_GET['funcs'] == 'yes'){
		fb(Trace::get_user_funcs(), 'funcs');
	}

	if(isset($_GET['userinfo']) && $_GET['userinfo'] == 'yes'){
		fb($__userinfo, 'userinfo');
	}

	if(isset($_GET['sysinfo']) && $_GET['sysinfo'] == 'yes'){
		fb($_SERVER, '_SERVER');
	}
}

/********************************************************************************
 * 显示系统信息页及登录判断
 ********************************************************************************/

function sysinfo($custom = array(), $url = '', $ttl = 0){
	global $vars, $__tpl, $__tpl_pth, $__tpl_dir, $__theme_dir, $__url_here, $__menus, $__menu_highlight, $__lang_dir, $__l, $__userinfo;
	global $__frm, $__mdl, $__do;
	global $__f_sysvars, $__f_goods_cats, $__f_econfig;
	$__tpl = array(
		'file'		=> '_i.html',
		'header'	=> '_header.tpl',
		'footer'	=> '_footer.tpl',
		'path'		=> '',
		'body'		=> 'sysinfo.html',
	);
	$defaults = array(
		'ptitle'	=> '系统信息',
		'title'		=> '操作成功',
		'btns'		=> null,		// array('支付' => './mdl=user')
		'detail'	=> '',			// <<<HTML
// 您的订单号：<b>1210160915123928</b>&nbsp;应付金额：<b>￥217</b>{$__br}
// 配送方式：城市快递 支付方式：易宝网上银行
// HTML
		'links'		=> null,		// array('支付' => './mdl=user')
		'synjs'		=> '',			// 同步
		'jsdoc'		=> '',			// JS语句
	);
	foreach ($defaults as $key => $item) {
		if(empty($custom[$key])){
			$custom[$key] = $item;
		}
	}
	$vars['_pageinfo']['title'] = $custom['ptitle'];
	
	if(empty($url)){
		$userid = & VS::get('userid');
		if(!empty($userid)){	// 注册或登录成功后才需要跳转处理
			$__url = get_session('url');
			if(!empty($__url)){
				$url = $__url;
				del_session('url');
			}else{
				switch($url){
					case 'pay':
						$url = WEB_DIR.'pay.html';
						break;
					default:
						$url = './';
						break;
				}
			}
		}
	}
	$custom['url'] = $url;
	$custom['ttl'] = $ttl;
	$custom['sec'] = ceil($ttl / 1000);
	$vars['sysinfo'] = $custom;
	
	url_here($custom['title'], '');
	url_here('系统信息', '');

	include_once dirname(__FILE__).'/_flush.php';
	exit;
}

// == exit();
function echo_msg($msg){
	echo $msg;exit;
}

function ajax_msg($result, $msg, $callback = 'callback', $synjs = '', $ttl = 2000){
	if($result == 'succ' || $result > 0){
		$__url = get_session('url');
		if(/*$synjs != '' && */!empty($__url)){	// 注册或登录成功后才需要跳转处理
			$url = $__url;
			del_session('url');
		}
	}else{
		$url = '';
	}
	if(!empty($msg)){
		//$msg = addslashes($msg);
		$msg = htmlspecialchars($msg);
	}
	$jsdir = JS_DIR;
	$cookie_pre = COOKIE_PRE;
	echo $html = <<<HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>AJAX</title>
		<script type="text/javascript" language="JavaScript" charset="utf-8" src="{$jsdir}func.js"></script>
		<script type="text/javascript" language="JavaScript" charset="utf-8">
		<!--
			var _s = {cookie_pre:'{$cookie_pre}'};
		//-->
		</script>
		{$synjs}
		<script type="text/javascript" language="JavaScript" charset="utf-8">
		<!--
			var params = {};
			params['result']	= '{$result}';
			params['msg']		= '{$msg}';
			params['url']		= '{$url}';
			params['ttl']		= '{$ttl}';
			try{
				//setTimeout(function(){
					window.parent.{$callback}(params);
				//}, {$ttl});
			}catch(e){}
		//-->
		</script>

	</head>
	<body>
		
		<!-- 这里是伪Ajax输出，您能看到这个，说明程序没有语法错误 -->
		这里是伪Ajax输出，您能看到这个，说明程序没有语法错误

	</body>
</html>
HTML
;
	exit;
}

// $type	ajax json callback normal
function islogin($type = 'normal', $url = 'login'){
	$userid = & VS::get('userid');
	if(empty($userid)){
		// 保存登录前地址
		$__url = str_replace('//', '/', './'.$_SERVER['REQUEST_URI']);
		set_session('url', $__url);
				
		switch($url){
			default:
				$url = './index.php?mdl=user&do=login';
		}
		switch($type){
			case 'callback':
				ajax_msg('warning', '您还未登录或登录已超时', 'callback_nologin', $url);
				break;
			case 'ajax':
				echo 'nologin';
				break;
			case 'json':
				echo json_encode(array(
					'result'	=> 'nologin',
					'errmsg'	=> '您还未登录或登录已超时',
					'url'		=> $url,
				));
				break;
			default:
				$custom = array(
						'ptitle'=> '登录提示',
						'title'	=> '您还未登录或登录已超时',
						'detail'=> <<<HTML
HTML
						,
						'btns'	=> array(
							'登录'	=> './index.php?mdl=user&do=login',
							'注册'	=> './index.php?mdl=user&do=register',
						),
				);
				sysinfo($custom, './index.php?mdl=user&do=login', 5000);				
		}
		exit;
	}
	return $userid;
}

function go404(){
	$GLOBALS['g_vars']['tpl_path'] = THR_PTH;
	require APP_PTH.'404.php';
	exit;
}

function url_here($text, $href = ''){
	global $__url_here;
	if (empty($href)){
		$__url_here[] = $text;
	} else {
		$href = str_replace('//', '/', $href);
		$__url_here[] = "<a href=\"{$href}\">{$text}</a>";
	}
}
?>